package com.edu.cdp.controller;

import com.edu.cdp.DTO.AddGoodsDTO;
import com.edu.cdp.DTO.PageDTO;
import com.edu.cdp.DTO.UpdateGoodDTO;
import com.edu.cdp.common.PageResult;
import com.edu.cdp.common.Result;
import com.edu.cdp.domain.Goods;
import com.edu.cdp.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/goods")
@Tag(name="商品相关接口")
@Slf4j
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    @Operation(summary = "获取商品列表")
    public Result<List<Goods>> list() {
        List<Goods> list = goodsService.list();
        if (list.size() > 0) {
            return Result.success(list);
        } else {
            return Result.error("商品列表为空");
        }
    }

    @PostMapping("/addGood")
    @Operation(summary = "新增商品",
            description = "此接口允许添加商品信息信息，包括基本信息及头像。\" +\n" +
                    "通过`file`参数上传商品图片文件，实现商品图片的修改。"
    )
    public Result<Goods> add(@ParameterObject AddGoodsDTO goods,
                                 @RequestParam(required = false)
                                 MultipartFile goodImage) {
        if (goods.getCategoryId() == null) {
            return Result.error("商品分类不能为空");
        } else {
            Goods good = goodsService.selectByCategoryId(goods.getCategoryId());
            if (good != null) {
                return Result.error("暂无该商品分类");
            }
        }

        Goods good = goodsService.add(goods, goodImage);

        if (good != null) {
            return Result.success(good);
        } else {
            return Result.error("该分类下商品已存在");
        }
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品")
    public Result<Goods> deleteGood(@RequestParam
                                        @Schema(description = "商品id")
                                        Integer id) {
        goodsService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update")
    @Operation(summary = "修改商品",
            description = "此接口允许修改商品部分信息，包括基本信息及图片"
            + "通过`file`参数上传商品图片文件，实现商品图片的修改。"
    )
    public Result<Goods> update(@RequestBody UpdateGoodDTO goods,
                                @RequestParam(required = false)
                                MultipartFile file){
        Goods newGood = goodsService.newUpdate(goods, file);
        if (newGood == null){
            return Result.error("暂无此商品");
        } else {
            boolean result = goodsService.updateById(newGood);
            if (result) {
               return Result.success(newGood);
            } else {
                return Result.error("修改失败");
            }
        }
    }

    @GetMapping("/listByTypeId")
    @Operation(summary = "根据分类id查询商品")
    public Result<List<Goods>> listByTypeId(@RequestBody
                                                @Parameter(name = "id", description = "商品分类id")
                                                Integer typeId) {
        List<Goods> list  = goodsService.listByTypeId(typeId);

        return Result.success(list);
    }

    @GetMapping("/stockShortage")
    @Operation(summary = "库存不足商品查询", description = "查询库存小于该数量（quantity）的商品）")
    public Result<List<Goods>> shortageGoods(@RequestBody
                                                 @Parameter(name = "quantity", description = "库存数量")
                                                 Integer quantity) {
        List<Goods> list = goodsService.selectByStock(quantity);

        return Result.success(list);
    }

    @GetMapping("/stockAmple")
    @Operation(summary = "库存充足商品查询", description = "查询库存大于该数量（quantity）的商品")
    public Result<List<Goods>> ampleGoods(@RequestBody
                                              @Parameter(name = "quantity", description = "库存数量")
                                              Integer quantity) {
        List<Goods> list = goodsService.selectByStockAmple(quantity);

        return Result.success(list);
    }

    @GetMapping("/listByName")
    @Operation(summary = "根据商品名称模糊查询商品")
    public Result<List<Goods>> listByName(@RequestBody
                                              @Schema(description = "商品名称")
                                              String name) {
        List<Goods> goodsList = goodsService.listByName(name);

        return Result.success(goodsList);
    }

    @GetMapping("/page")
    @Operation(summary = "商品分页查询")
    public Result<PageResult> page(@RequestBody PageDTO goodsPageDTO) {
        PageResult pageResult   = goodsService.pageQuery( goodsPageDTO);
        return Result.success(pageResult);
    }

    @PutMapping("/addStock")
    @Operation(summary = "增加商品库存")
    public Result<Goods> addStock(@RequestBody
                                      @Parameter(name = "id", description = "商品id")
                                    Integer id,
                                  @Parameter(name = "quantity", description = "添加数量")
                                    @RequestBody Integer quantity) {
        Goods goods = goodsService.addStock(id, quantity);
        if (goods == null) {
            return Result.error("库存添加失败");
        } else {
            return Result.success(goods);
        }
    }
}
